package com.as.client.charts;

import java.sql.Timestamp;
import java.util.ArrayList;

import com.as.client.services.SimpleService;
import com.as.client.services.SimpleServiceAsync;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.rpc.ServiceDefTarget;
import com.google.gwt.user.client.ui.ChangeListener;
import com.google.gwt.user.client.ui.ChangeListenerCollection;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.Widget;
import com.gwtext.client.data.ArrayReader;
import com.gwtext.client.data.MemoryProxy;
import com.gwtext.client.data.RecordDef;
import com.gwtext.client.data.Store;
import com.gwtext.client.widgets.Panel;
import com.gwtext.client.widgets.chart.yui.LineChart;
import com.gwtext.client.widgets.chart.yui.SeriesDefY;
import com.gwtext.client.widgets.layout.VerticalLayout;

public class LostChart extends Panel implements ChangeListener{
	
	ArrayList<ArrayList<Double>> _lostList=null; 
	ArrayList<ArrayList<Timestamp>> _timeList=null;
	private ChangeListenerCollection _changeListeners;
	RecordDef _recordDef;
	SeriesDefY[] _seriesDef;
	static int _numInt=0; 
	Chart c = new Chart();
	
	public LostChart(){
		final SimpleServiceAsync simpleService = (SimpleServiceAsync) GWT.create(SimpleService.class);
		String moduleRelativeURL = GWT.getModuleBaseURL() + "simpleservice";
		((ServiceDefTarget)simpleService).setServiceEntryPoint(moduleRelativeURL);
		
		this.addChangeListener(this);
		
		
		//****************************************get the lost list from the server****************************/////////////////////////// 
		simpleService.getLostList(new AsyncCallback<ArrayList<ArrayList<Double>>>() {
			
			public void onSuccess(ArrayList<ArrayList<Double>> arg0) {
				_lostList= arg0; 
				if(_lostList==null){
					Window.alert("lost list null");
				}
				else{
					_numInt = _lostList.size();
				
					// ************************************************if the lost is received: get the time List***********//
					
					// get the time list from the server 
					
					simpleService.getTimeList(new AsyncCallback<ArrayList<ArrayList<Timestamp>>>() {
						
						public void onSuccess(ArrayList<ArrayList<Timestamp>> arg0) {
							
							_timeList= arg0; 
							if(_timeList==null){
								Window.alert("time list null");
							}
							else{
								//***********************************************if the time and the lost are received: fire change********//
								if (_changeListeners != null) {
				                     _changeListeners.fireChange(RootPanel.get());
				             }
							
							
						}
							
						}
						public void onFailure(Throwable arg0) {
							Window.alert("failure timeList");
							
						}
					});
					
				}
				
			}
			
			public void onFailure(Throwable arg0) {
				Window.alert("failure lost List");
				
			}
		});
		
	}


	public void onChange(Widget arg0) {
		this.setPaddings(15);
		this.setLayout(new VerticalLayout(15));
		ArrayList l = c.GetSize(_lostList); 
		MemoryProxy proxy = new MemoryProxy(c.getData(_lostList, _timeList,l,_numInt));
		_recordDef=c.createRecord(_numInt, "lost%");
		_seriesDef=c.createSerie(_numInt, "lost%");
		ArrayReader reader = new ArrayReader(_recordDef);
		final Store store = new Store(proxy, reader);
		 store.load(); 
		 final LineChart chart = new LineChart(); 
		 chart.setTitle("% Lost"); 
		 chart.setWMode("transparent"); 
		 chart.setStore(store); 
		 chart.setSeries(_seriesDef); 
		 chart.setXField("time"); 
		 chart.setYField("lost%");
		 chart.setExpressInstall("js/yui/assets/expressinstall.swf");
		 chart.setWidth(350);
		 chart.setHeight(250);
		 chart.setAutoScroll(true);
		 chart.setVisible(true);
		 this.add(chart);
		 this.setAutoHeight(true);
		 this.setAutoWidth(true);
		 this.setAutoScroll(true);
		 this.setVisible(true);
		 this.doLayout();
		
	}
	  public void addChangeListener(ChangeListener listener) {
          if (_changeListeners == null)
                  _changeListeners = new ChangeListenerCollection();
          _changeListeners.add(listener);
  }

}
